<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\admin\controller;

class File extends \app\AdminRest
{
    public $uid = 0;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->uid = 0;
    }
    public function createGroup()
    {
        $input = $this->_input;
        if(!isset($input["group"]["name"]) && !isset($input["name"])) {
            return $this->error("not group name ,please check .");
        }
        $group["name"] = isset($input["group"]["name"]) ? $input["group"]["name"] : $input["name"];
        $group["uniacid"] = $this->_uniacid;
        $group["admin_id"] = $this->_user["id"];
        $group_model = new \app\admin\model\AttachmentGroup();
        $group_count = $group_model->where(["uniacid" => $this->_uniacid, "admin_id" => $this->_user["id"]])->count("id");
        if(20 <= $group_count) {
            return $this->error("分组最多限制为20个！");
        }
        $repeat = $group_model->where(["uniacid" => $this->_uniacid, "name" => $group["name"], "admin_id" => $this->_user["id"]])->count();
        if($repeat) {
            return $this->error("已存在同名分组");
        }
        $result = $group_model->createGroup($group);
        return $this->success($result);
    }
    public function listGroup()
    {
        $param = $this->_param;
        if(isset($param["name"])) {
            $filter[] = ["name", "=", $param["name"]];
        }
        $filter[] = ["uniacid", "=", $this->_uniacid];
        if(empty($this->_user["is_admin"])) {
            $filter[] = ["admin_id", "=", $this->_user["id"]];
        } else {
            $filter[] = ["admin_id", "in", [$this->_user["id"], 0]];
        }
        $group_model = new \app\admin\model\AttachmentGroup();
        $result = $group_model->where($filter)->select()->toArray();
        return $this->success(["groups" => $result]);
    }
    public function updateGroup()
    {
        $group_id = $this->_param["group_id"];
        $data = $this->_input["group"];
        $group_model = new \app\admin\model\AttachmentGroup();
        $repeat = $group_model->where(["uniacid" => $this->_uniacid, "name" => $data["name"]])->where("id", "<>", $group_id)->count();
        if($repeat) {
            return $this->error("已存在同名分组");
        }
        $result = $group_model->updateGroup(["id" => $group_id, "uniacid" => $this->_uniacid], $data);
        return $this->success($result);
    }
    public function delGroup()
    {
        if(!isset($this->_param["group_id"])) {
            return $this->error("not group id");
        }
        $group_id = $this->_param["group_id"];
        $where = [["group_id", "=", $group_id], ["uniacid", "=", $this->_uniacid]];
        $attachment_model = new \app\admin\model\CoreAttachment();
        $file_count = $attachment_model->where($where)->count("id");
        if(0 < $file_count) {
            return $this->error("目前分组中有文件数据，不可删除，如要删除，请先清除文件数据");
        }
        $group_model = new \app\admin\model\AttachmentGroup();
        $result = $group_model->delGroup(["id" => $group_id]);
        return $this->success($result);
    }
    public function delAllGroup()
    {
        if(!isset($this->_param["group_id"])) {
            return $this->error("not group id");
        }
        $group_id = $this->_param["group_id"];
        $attachment_model = new \app\admin\model\CoreAttachment();
        if(is_array($group_id)) {
            foreach ($group_id as $id) {
                $where = [["group_id", "=", $id], ["uniacid", "=", $this->_uniacid]];
                $file_count = $attachment_model->where($where)->count("id");
                if(0 < $file_count) {
                    return $this->error("目前分组中有文件数据，不可删除，如要删除，请先清除文件数据");
                }
            }
            $group_model = new \app\admin\model\AttachmentGroup();
            $result = $group_model->delGroup(["id" => $group_id]);
            return $this->success($result);
        } else {
            return $this->delGroup();
        }
    }
    public function uploadFile()
    {
        $input = $this->_param;
        $file = $this->request->file("file");
        if(empty($file)) {
            return $this->error("not file ,please check file.");
        }
        $uploda_model = new \app\Common\Upload($this->_uniacid);
        $type = "picture";
        if(isset($input["type"])) {
            $type = $input["type"];
        }
        $info = $uploda_model->upload($type, $file);
        $result = false;
        if(!empty($info)) {
            $info["uid"] = $this->uid;
            $info["admin_id"] = $this->_user["id"];
            $info["longbing_attachment_path"] = longbingGetFilePath($info["attachment"], $this->_host, $this->_uniacid);
            $info["longbing_from"] = "web";
            $attachment_model = new \app\admin\model\CoreAttachment();
            $result = $attachment_model->createAttach($info);
            if(!empty($result)) {
                $result = $info;
            }
        }
        return $this->success($result);
    }
    public function uploadFiles()
    {
        $input = $this->_param;
        $files = $this->request->file("file");
        if(empty($files)) {
            return $this->error("not file ,please check file.");
        }
        $type = "picture";
        if(isset($input["type"])) {
            $type = $input["type"];
        }
        $group_id = -1;
        if(isset($input["group_id"])) {
            $group_id = $input["group_id"];
        }
        $result = [];
        $uploda_model = new \app\Common\Upload($this->_uniacid);
        foreach ($files as $file) {
            $info = $uploda_model->upload($type, $file);
            if(!empty($info)) {
                $info["uid"] = $this->uid;
                $info["admin_id"] = $this->_user["id"];
                $info["attachment_path"] = longbingGetFilePath($info["attachment"], $this->_host, $this->_uniacid, $info["longbing_driver"]);
                $info["group_id"] = $group_id;
                $info["uniacid"] = $this->_uniacid;
                $attachment_model = new \app\admin\model\CoreAttachment();
                $data = $attachment_model->createAttach($info);
                if(!empty($data)) {
                    $result[] = $info;
                }
            }
        }
        return $this->success($result);
    }
    public function listFile()
    {
        $param = $this->_param;
        $filter["uniacid"] = $this->_uniacid;
        if(!empty($param["type"])) {
            $filter["type"] = $param["type"];
        } else {
            $filter["type"] = 1;
        }
        $filter["uid"] = 0;
        if(empty($this->_user["is_admin"])) {
            $where[] = ["admin_id", "=", $this->_user["id"]];
        } else {
            $where[] = ["admin_id", "in", [$this->_user["id"], 0]];
        }
        if(isset($param["group_id"])) {
            $filter["group_id"] = $param["group_id"] ? $param["group_id"] : ["0", "-1"];
        }
        if(isset($param["type"]) && in_array($param["type"], [1, 2, 3])) {
            $filter["type"] = $param["type"];
        }
        $file_model = new \app\admin\model\CoreAttachment();
        $result = $file_model->where($filter)->where($where)->order("id desc")->paginate($param["limit"])->toArray();
        $result["files"] = $result["data"];
        unset($result["data"]);
        $result["files"] = transImagesOne($result["files"], ["attachment"], $this->_uniacid);
        return $this->success($result);
    }
    public function getFile()
    {
        $param = $this->_param;
        $filter["uniacid"] = $this->_uniacid;
        $filter["id"] = $param["id"];
        $file_model = new \app\admin\model\CoreAttachment();
        $file = $file_model->getFile($filter);
        return $this->success($file);
    }
    public function delFile()
    {
        $input = $this->_input;
        if(!isset($input["ids"])) {
            return $this->error("not file id,please check.");
        }
        $filter["ids"] = $input["ids"];
        $filter["uniacid"] = $this->_uniacid;
        $file_model = new \app\admin\model\CoreAttachment();
        $result = $file_model->delAttach($filter);
        return $this->success($result);
    }
    public function uploadConfig()
    {
        $data = longbingGetOssConfig($this->_uniacid);
        $data["uniacid"] = $this->_uniacid;
        return $this->success($data);
    }
    public function addFile()
    {
        $info = $this->_input;
        $group_id = -1;
        if(isset($info["group_id"])) {
            $group_id = $info["group_id"];
        }
        $info["uid"] = $this->uid;
        $info["group_id"] = $group_id;
        $info["admin_id"] = $this->_user["id"];
        $info["uniacid"] = $this->_uniacid;
        $info["createtime"] = time();
        $attachment_model = new \app\admin\model\CoreAttachment();
        $data = $attachment_model->addAttach($info);
        return $this->success($data);
    }
    public function moveGroup()
    {
        $input = $this->_input;
        $attachment_model = new \app\admin\model\CoreAttachment();
        $res = $attachment_model->where("id", "in", $input["file_id"])->update(["group_id" => $input["group_id"]]);
        return $this->success($res);
    }
}

?>